package com.jiazhong.boot.security.model;

import com.jiazhong.boot.commons.bean.plus.SecurityUser;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
@NoArgsConstructor
public class SecurityUserDetails implements UserDetails, Serializable {


    // 封装了用户对象
    private SecurityUser securityUser;
    // 封装了用户的权限列表
    private List<GrantedAuthority> grantedAuthorities;


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
/*
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        this.grantedAuthorities.forEach(e -> {
            GrantedAuthority authority = new SimpleGrantedAuthority(e);
            grantedAuthorities.add(authority);
        });
*/

        return grantedAuthorities;
    }

    @Override
    public String getPassword() {
        return securityUser.getPassword();
    }

    @Override
    public String getUsername() {
        return securityUser.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
